package cn.org.mrliu.ssm.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CollectionUtil {
	public static Object[] split(Collection<?> collection, int interval) {
		if (collection == null || collection.size() == 0) {
			return null;
		}
		if (interval == 0) {
			return null;
		}
		// 获取到集合大小
		int size = collection.size();
		// 将集合拆分成几个部分
		int part = size % interval == 0 ? size / interval : size / interval + 1;
		int fromIndex = 0; // 拆分的开始索引
		int toIndex = 0; // 拆分的结束索引
		Object[] objects = new Object[part];
		// 临时List集合
		List<?> tempList = new ArrayList<>(collection);
		for (int i = 0; i < part; i++) {
			// 如果是最后一部分,结束索引为集合size
			if (i + 1 == part) {
				fromIndex = i * interval;
				toIndex = size;
			} else {
				fromIndex = i * interval;
				toIndex = interval * (i + 1);
			}
			objects[i] = tempList.subList(fromIndex, toIndex);
		}
		return objects;
	}
}
